<?php

namespace app\admin\controller;


use app\admin\model\SystemAdmin;
use app\admin\service\AuthService;
use app\common\controller\AdminController;

/**
 * Class Ajax
 * @package app\admin\controller
 */
class Ajax extends AdminController
{
    /*获取所有权限节点*/
    public function getAllAuthNode() {
        $authService = app(AuthService::class, ['adminId' => $this->admin_id, 'authIds'=> $this->adminInfo['auth_ids']??'']);
        $list = $authService->getAdminNode();
        $data = [
            'nodes'=> $list,
            'isSuper'=> $this->admin_id == config('admin.super_admin_id'),
        ];
        $this->success($data);
    }

    /**
     * 管理员列表
     * @return string
     * @throws \Exception
     */
    public function adminList()
    {
        $adminDb = new SystemAdmin();
        $list = $adminDb->where('status', 1)->column('username');
        $this->success(['list'=> $list, 'admin_name'=> $this->admin_name]);
    }

    /*上传图片*/
    public function upload() {
        $file = request()->file('file');
        $basedir = 'uploads';
        if(!is_dir($basedir)){
            mkdir($basedir,0777,true);
        }
        $saveName = \think\facade\Filesystem::disk('public')->putFile( $basedir, $file);
        $saveName = str_replace('\\', '/', $saveName);
        $url = request()->domain().'/'.$saveName;
        $name = explode('/', $saveName);
        $name = end($name);
        $this->success(['url'=> $url, 'name'=> $name]);
    }

    /**
     * 修改用户密码
     * @return mixed
     */
    public function editPassword()
    {
        $password = input('password', '', 'trim');
        $newPassword = input('newPassword', '', 'trim');
        if (empty($password) || empty($newPassword)) {
            $this->error('参数缺失');
        }
        if ($this->adminInfo['password'] != password($password)) {
            $this->error('旧密码不正确');
        }
        $update = [
            'password'=> password($newPassword),
        ];
        $adminDb = new SystemAdmin();
        $adminDb->where(['id'=> $this->admin_id])->update($update);
        $this->success([], '修改密码成功');
    }
}
